#include "Entity.hpp"

Entity::Entity(){
	x = y = 0;
	velocityX = velocityY = 0.00;
	maxVelocityX = maxVelocityY = 1.0;
}

Entity::Entity(float entityX, float entityY){
	x = entityX; y = entityY;
	lastNetworkUpdateTime = 0;
	velocityX = velocityY = 0.00;
}

/** 
* Sets the entity to move in direction dir.
* 0 for not moving.
* @param dir Direction to move in.
*/
void Entity::setMove(byte dir){
    direction = dir;
    
    if(direction)
        moving = true;
    else
        moving = false;
}

void Entity::move(){
    moveX();
    moveY();
}

/**
 * Update entity's horizontal velocity and position.
 */
void Entity::moveX(){
    //If entity is moving in a horizontal direction.
    if(direction & Entity::dir_Horizontal){

        //Check for direction and adjust velocity.
		if(direction & Entity::dir_E)
			if(velocityX < maxVelocityX)
				velocityX += acceleration;

		if(direction & Entity::dir_W)
			if(velocityX > -maxVelocityX)
				velocityX -= acceleration;
	}else{
        //Entity is not moving horizontally but still has
        //horizontal velocity.
		if(velocityX != 0){
			if(abs(velocityX) <= acceleration){
				velocityX = 0;
			}else{
				if(velocityX > 0)
					velocityX -= acceleration;
				else
					velocityX += acceleration;
			}
		}
	}
    x += velocityX;
}

/**
 * Update entity's vertical velocity and position.
 */
void Entity::moveY(){
    //See above comments.
    if(direction & Entity::dir_Vertical){
		if(direction & Entity::dir_N)
			if(velocityY > -maxVelocityY)
				velocityY -= acceleration;

		if(direction & Entity::dir_S)
			if(velocityY < maxVelocityY)
				velocityY += acceleration;
		
	}else{
		if(velocityY != 0){
			if(abs(velocityY) <= acceleration){
				velocityY = 0;
			}else{
				if(velocityY > 0)
					velocityY -= acceleration;
				else
					velocityY += acceleration;
			}
		}
	}
    y += velocityY;
}